﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_Inspection : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private List<string> customer;

        public panel_Inspection()
        {
            InitializeComponent();            

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.customer = new List<string>();

            LoadSerial();
            LoadCustomer();            
        }
                
        private void LoadSerial()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_RobotOrderDetail] ORDER BY [SerialNo]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["SerialNo"].ToString().Trim();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    this.serialNoFrom.Properties.Items.Add(item);
                    this.serialNoTo.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get SerialNo list");
            }
        }

        private void LoadCustomer()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CustomerID FROM [231_Customer] ORDER BY CustomerID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CustomerID"].ToString().Trim();
                    item.Value = dataReader["CustomerID"].ToString().Trim();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Customer list");
            }
        } 

        private void buttonView_Click(object sender, EventArgs e)
        {
            bool pass = true;
            string select_customer = null;
            if ((this.serialNoFrom.SelectedIndex < 0) || (this.serialNoTo.SelectedIndex < 0))
            {
                MessageBox.Show("Please select Serial to view report");
                pass = false;
            }

            if (pass)
            {
                if (this.customerID.SelectedIndex < 0)
                {
                    MessageBox.Show("Please select Customer");
                    pass = false;
                }
                else
                {                    
                    bool check_cus = true;
                    ComboboxItem item = (ComboboxItem)this.customerID.SelectedItem;
                    select_customer = item.Value.ToString().Trim();
                    if (customer.Count > 0)
                    {
                        for (int i = 0; i < customer.Count; i++)
                        {
                            if (customer[i] != select_customer)
                            {
                                check_cus = false;
                            }
                        }
                    }
                    if (!check_cus)
                    {
                        MessageBox.Show("Selected customer is different from Details Table");
                    }
                }
            }

            if (pass)
            {
                if (string.IsNullOrEmpty(this.shippingDate.EditValue.ToString()))
                {
                    MessageBox.Show("Please select Shipping Date");
                    pass = false;
                }
            }

            if (pass)
            {                
                ComboboxItem from_serial = (ComboboxItem)serialNoFrom.SelectedItem;
                ComboboxItem to_serial = (ComboboxItem)serialNoTo.SelectedItem;
                reportDocument.DocumentSource = null;
                rpt_Inspection Inspection = new rpt_Inspection();
                Inspection.serialno_from = from_serial.Value.ToString();
                Inspection.serialno_to = to_serial.Value.ToString();
                Inspection.shippingdate = this.shippingDate.DateTime.ToString("dd.MM.yyyy");
                Inspection.customer = select_customer;
                Inspection.LoadData();
                Inspection.CreateDocument();
                reportDocument.DocumentSource = Inspection;
            }  
        }

        private void serialNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            customer.Clear();
            if (this.serialNoFrom.SelectedIndex >= 0)
            {
                ComboboxItem ser = (ComboboxItem)this.serialNoFrom.SelectedItem;
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT CustomerID FROM [231_RobotOrderDetail]"
                        + " WHERE SerialNo = '" + ser.Value.ToString() + "'"
                        + " UNION ALL"
                        + " SELECT CustomerID FROM [231_F5Detail]"
                        + " WHERE SerialNo = '" + ser.Value.ToString() + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    while (dataReader.Read())
                    {
                        customer.Add(dataReader["CustomerID"].ToString().Trim());
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();
                }
                catch
                {
                    customer.Clear();
                }
                finally
                {
                    myConnection.Close();
                }
            }
            this.serialNoTo.SelectedIndex = this.serialNoFrom.SelectedIndex;
        }
    }
}
